package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.contract.AddDriverContract;
import com.bocai.kelalacar.ui.car.set.module.AddDriverModule;
import com.bocai.kelalacar.ui.car.set.presenter.AddDriverPresenter;
import com.bocai.kelalacar.util.SP;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/23.
 */

public class AddDriverActivity extends BaseActivity implements AddDriverContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @Inject
    AddDriverPresenter presenter;
    String name,phone,id;

    @Override
    public int getLayoutId() {
        return R.layout.act_add_driver;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new AddDriverModule(this)).inject(this);
    }

    @Override
    public void initView() {

        name = getIntent().getStringExtra("name");
        phone = getIntent().getStringExtra("phone");
        id = getIntent().getStringExtra("id");

        initToolbar("司机管理");
        initBack(imgBack,llBack);
        llRight.setVisibility(View.VISIBLE);
        tvRight.setText("保存");

        if (name!=null&&id!=null&&!id.equals("")){
            edName.setText(name);
            edPhone.setText(phone);
        }

    }
    @OnClick({R.id.ll_right})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.ll_right:
                if (TextUtils.isEmpty(edName.getText().toString())){
                    showErrorTip("请输入司机姓名");
                }else if (TextUtils.isEmpty(edPhone.getText().toString())){
                    showErrorTip("请输入电话号码");
                }else {
                    if (name!=null&&id!=null&&!id.equals("")){
                        presenter.modifyDriver(id,edName.getText().toString(),edPhone.getText().toString());
                    }else {
                        presenter.addDriver(SP.get(this, "id", "").toString(), edName.getText().toString(), edPhone.getText().toString());
                    }
                }
                break;
        }
    }

    @Override
    public void addDriverSuccess() {
        showErrorTip("添加司机成功");
        finish();
    }

    @Override
    public void getDriverList(CarDriverBean bean) {

    }

    @Override
    public void deleteDriverSuccess(int index) {

    }

    @Override
    public void modifyDriverSuccess() {
        showErrorTip("修改司机成功");
        finish();
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }
}
